package com.ocm.commerceinteraction.util.helper;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

import com.ocm.commerceinteraction.GlobalApplication;
import com.ocm.commerceinteraction.model.CenterMessage;
import com.ocm.commerceinteraction.model.FriendsList;
import com.ocm.commerceinteraction.model.UserDetailInfo;
import com.ocm.commerceinteraction.model.UserInfo;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 本地数据库操作类
 *
 * @author tl
 * @Date 2015-3-12
 */
public class DBHelper {


    /**
     * 添加系统推送消息
     */
    public static boolean addCenterMsg(CenterMessage info) {
        info.setMsgId(System.currentTimeMillis());
        info.setState(0);
            String userId = GlobalApplication.loginModel.getData().getUser_uniq_id();
            info.setUserId(Integer.parseInt(userId));
        boolean b = info.save();
        return b;
    }



	/**
	 * 修改系统推送消息状态
	 */
	public static void updateCenterMsgState(CenterMessage info)
	{
		String userId = GlobalApplication.loginModel.getData().getUser_uniq_id();
		ContentValues values = new ContentValues();
        values.put("state", "1");
		DataSupport.updateAll(CenterMessage.class, values, "userId = ? and msgId = ? ",userId+"", info.getMsgId()+"");
	}

    /**
     * 获取系统推送消息
     */
    public static List<CenterMessage> getCenterMsg() {
        String userId = GlobalApplication.loginModel.getData().getUser_uniq_id();
        List<CenterMessage> list = DataSupport.where(" userId = ? and state = ?", userId, "0").order("creatTime desc").limit(100).offset(0).find(CenterMessage.class);
        if (null == list)
            list = new ArrayList<CenterMessage>();

//        ContentValues values = new ContentValues();
//        DataSupport.updateAll(CenterMessage.class, values, "userId = ? ", userId + "");
        return list;
    }


    /**
     * 添加好友列表
     */
    public static boolean addFriendList(FriendsList info) {
        boolean b = true;
        for (int i = 0; i < info.getData().size(); i++) {
            if (getFriendDetail(info.getData().get(i).getUser_uniq_id()) == null) {
                b = b && info.getData().get(i).save();
            }
        }
        return b;
    }

    /**
     * 获取好友列表
     */
    public static List<UserInfo> getFriendlist() {
        List<UserInfo> list = DataSupport.findAll(UserInfo.class);
        if (null == list)
            list = new ArrayList<UserInfo>();
        return list;
    }

    /**
     * 删除好友列表
     */
    public static int removeMoveFriendList(){
        int n = DataSupport.deleteAll(UserInfo.class);
        return n;
    }


    /**
     * 获取登录用户课程视频信息
     */
    public static List<UserInfo> getFriendDetail(String user_uniq_id)
    {
        List<UserInfo> list = DataSupport.where("user_uniq_id = ? ", user_uniq_id).find(UserInfo.class);
//        if (null == list)
//        {
//            list = new ArrayList<UserInfo>();
//        }
        return list;
    }

}
